package com.runa.rccprotocol.dto.debao;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;
import com.runa.rccprotocol.dto.runa.RunaFrameSequence;

import static com.runa.rccprotocol.dto.runa.RunaFrameSequence.pn;

public class DeBaoRequestData extends ConcentratorRequestData {

    public DeBaoRequestData(String address) {
        super(address);
    }

    private byte[] requestData;

    public static void main(String[] args) {
        DeBaoRequestData deBaoRequestData = new DeBaoRequestData("103611361");
//        deBaoRequestData.readTime();
//        deBaoRequestData.readHeatMeterByIndex(1);
//        deBaoRequestData.readHeatMeterByIndex(18);
        deBaoRequestData.readByTime(ByteUtil.hexStringToByte("00 08 09 12 24"), (byte)0x02,  22);
        System.out.println(ByteUtil.byteToHexString(deBaoRequestData.getData()));
    }

    public void readByTime(byte[] readTime, byte factoryCode, int pn) {
        readByTime(readTime, factoryCode, pn, 223);
    }

    public byte[] readTime() {
        String s = "68 31 00 31 00 68 " +
                "4B 08 10 18 27 02 " +
                "0C 60 00 00 04 16 " +
                "2A 16";
        requestData = ByteUtil.hexStringToByte(s);


        ByteUtil.tenstringToBcd(requestData, address.substring(address.length() - 4), 7, false);
        String format = String.format("%x", Short.parseShort(address.substring(0, 5)));
        requestData[9] = ByteUtil.hexStringToByte(format.substring(2,4))[0];
        requestData[10] = ByteUtil.hexStringToByte(format.substring(0,2))[0];

        requestData[requestData.length-2] = ByteUtil.sum(requestData, 6, requestData.length-2-6);

        setHaveResult(true);
        return requestData;
    }

    public void readByTime(byte[] readTime, byte factoryCode, int pn, int fn) {
        String ss = "68 4E 00 4E 00 68 " +
                "4B 70 13 82 28 02 " +
                "0D 60 01 01 40 1B " +
                "00 20 12 05 24 " +
                "03 01 " +
                "A3 16";
        requestData = ByteUtil.hexStringToByte(ss);

        ByteUtil.tenstringToBcd(requestData, address.substring(address.length() - 4), 7, false);
        String format = String.format("%x", Short.parseShort(address.substring(0, 5)));
        requestData[9] = ByteUtil.hexStringToByte(format.substring(2,4))[0];
        requestData[10] = ByteUtil.hexStringToByte(format.substring(0,2))[0];
        requestData[11] = factoryCode;

        byte[] bytes = RunaFrameSequence.pnToBin(pn);
        int index = 14;
        for (byte aByte : bytes) {
            requestData[index++] = aByte;
        }
        byte[] bytes1 = RunaFrameSequence.fnToBin(fn);
        for (byte aByte : bytes1) {
            requestData[index++] = aByte;
        }
        for (byte b : readTime) {
            requestData[index++] = b;
        }

        requestData[requestData.length-2] = ByteUtil.sum(requestData, 6, requestData.length-2-6);
        setHaveResult(true);
    }

    public void readHeatMeterByIndex(int pn) {
        requestData = new byte[]{ (byte) 0x68, (byte) 0x32, (byte) 0x00, (byte) 0x32, (byte) 0x00, (byte) 0x68
                , 0x4A, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0C, 0x60, 0x00, 0x00, 0x02, 0x16, 0x00, 0x16 };
        ByteUtil.tenstringToBcd(requestData, address.substring(address.length() - 4), 7, false);
        String format = String.format("%x", Short.parseShort(address.substring(0, 5)));
        requestData[9] = ByteUtil.hexStringToByte(format.substring(2,4))[0];
        requestData[10] = ByteUtil.hexStringToByte(format.substring(0,2))[0];
        requestData[11] = 2;
        byte[] pnBytes = RunaFrameSequence.pnToBin(pn);
        System.arraycopy(pnBytes, 0, requestData, 14, pnBytes.length);
        byte[] fnBytes  = RunaFrameSequence.fnToBin(178);
        System.arraycopy(fnBytes, 0, requestData, 16, pnBytes.length);
        requestData[requestData.length-2] = ByteUtil.sum(requestData, 6, requestData.length-6-2);
        setHaveResult(true);
    }

    public void readHeatMeter(String meterNo) {
//        requestData = new byte[]{(byte) 0xFE, (byte) 0xFE, (byte) 0xFE, (byte) 0xFE,
//                0x68,0x20,
//                0,0,0,0,
//                0x0,0x11,0x11,0x01,0x03, 0x1F, (byte) 0x90,0x00,
//                0,
//                0x16};
        requestData = new byte[]{(byte) 0x68, (byte) 0xDA, (byte) 0x00, (byte) 0xDA, (byte) 0x00,
                0x68, 0x4A, 0, 0, 0, 0, 0x02,
                0x10, 0x65, 0x00, 0x00, 0x01, 0x00, 0x1F, 0x6B, (byte) 0x82, 0x20, 0x14, 0x00,
                (byte) 0xFE, (byte) 0xFE, (byte) 0xFE, (byte) 0xFE, 0x68, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x11, 0x11,
                0x01, 0x03, 0x1F, (byte) 0x90, 0x00, 0x00, 0x16,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00,0x16};
        ByteUtil.tenstringToBcd(requestData, address.substring(address.length() - 4), 7, false);
        String format = String.format("%x", Short.parseShort(address.substring(0, 5)));
        requestData[9] = ByteUtil.hexStringToByte(format.substring(2,4))[0];
        requestData[10] = ByteUtil.hexStringToByte(format.substring(0,2))[0];
//        ByteUtil.tenstringToBcd(requestData, format, 9, false);
//        char[] chars = format.toCharArray();
//        requestData[9] = (byte) ((chars[2] - 0x30<<4) | (chars[3] - 0x30)&0x0f);
//        requestData[10] = (byte) ((chars[0] - 0x30<<4) | (chars[1] - 0x30)&0x0f);

        ByteUtil.tenstringToBcd(requestData, meterNo, 30, false);

        requestData[42] = ByteUtil.sum(requestData, 28, 14);
        requestData[requestData.length-2] = ByteUtil.sum(requestData, 6, requestData.length-2-6);
        setHaveResult(true);
    }


    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    @Override
    public byte[] getData() {
        return this.requestData;
    }
}
